package charles.testproject.xmlparser;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.w3c.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DOMParser {
	private static Logger logger = Logger.getLogger(DOMParser.class);
	public static void main(String[] args) {
		PropertyConfigurator.configure("log/log4jpropertiesfile/log4j.properties");
		System.out.println("***************************DOM PARSER***************************");
		long startTime = System.currentTimeMillis();
		try {
			logger.debug(" was called!");
			File f = new File("doc/ParserTest.xml");
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder  docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(f);
			NodeList nl = doc.getElementsByTagName("destination");
			for(int i = 0; i < nl.getLength(); i++) {
				System.out.println("Yesterday=" + doc.getElementsByTagName("yesterday").item(i).getFirstChild().getNodeValue());
				System.out.println("Today=" + doc.getElementsByTagName("today").item(i).getFirstChild().getNodeValue());
				System.out.println("Tomorrow=" + doc.getElementsByTagName("tomorrow").item(i).getFirstChild().getNodeValue());
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long endTime = System.currentTimeMillis();
		System.out.println("****DOMParser cost time = " + (endTime - startTime));
		System.out.println("***************************DOM PARSER***************************");
	}
}
